//go:build deprecated
// +build deprecated

package rpcserver

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/containersvc"
)

const (
	docker = rpcv1.RPC_SERVICE_CONTAINER
)

func init() {
	depend.Provide(NewDockerRouter)
	registerRouter(docker, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *dockerRouter) {
			d.initRouter(c)
		})
	})
}

type dockerRouter struct {
	docker *containersvc.Service
}

func NewDockerRouter(service *containersvc.Service) *dockerRouter {
	return &dockerRouter{
		docker: service,
	}
}

func (r *dockerRouter) initRouter(c rpcserver.IRoutes) {

	c.Group(rpcv1.CONTAINER_RESOURCE).
		Func(`create`, r.createContainer).
		Func(`get`, r.getContainer).
		Func(`delete`, r.deleteContainer).
		Func(`update`, r.updateContainer)
}

func (r *dockerRouter) createContainer(c *rpcserver.Context) {
	container := new(corev1.DockerContainer)
	if err := c.BindJSON(container); err != nil {
		return
	}

	result := r.docker.CreateContainer(container)
	c.JSON(result)
}

func (r *dockerRouter) deleteContainer(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.docker.DeleteContainer(meta)
	c.JSON(result)
}

func (r *dockerRouter) getContainer(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.docker.GetContainer(meta)
	c.JSON(result)
}

func (r *dockerRouter) updateContainer(c *rpcserver.Context) {
	container := new(corev1.DockerContainer)
	if err := c.BindJSON(container); err != nil {
		return
	}

	result := r.docker.UpdateContainer(container)
	c.JSON(result)
}
